<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>urlcapture.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
  </script>

  <script src="static/js/doc.js">
  </script>

  <meta charset="utf8">
</head>

<body onload="prettyPrint()">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_gears_urlcapture.js.html">urlcapture.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line2"></a>// you may not use this file except in compliance with the License.
<a name="line3"></a>// You may obtain a copy of the License at
<a name="line4"></a>//
<a name="line5"></a>//     http://www.apache.org/licenses/LICENSE-2.0
<a name="line6"></a>//
<a name="line7"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line8"></a>// distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
<a name="line9"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line10"></a>// See the License for the specific language governing permissions and
<a name="line11"></a>// limitations under the License.
<a name="line12"></a>
<a name="line13"></a>// Copyright 2007 Google Inc. All Rights Reserved.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Interface for capturing URLs to a ResourceStore on the
<a name="line17"></a> * LocalServer.
<a name="line18"></a> *
<a name="line19"></a> */
<a name="line20"></a>
<a name="line21"></a>
<a name="line22"></a>goog.provide(&#39;goog.gears.UrlCapture&#39;);
<a name="line23"></a>goog.provide(&#39;goog.gears.UrlCapture.Event&#39;);
<a name="line24"></a>goog.provide(&#39;goog.gears.UrlCapture.EventType&#39;);
<a name="line25"></a>
<a name="line26"></a>goog.require(&#39;goog.Uri&#39;);
<a name="line27"></a>goog.require(&#39;goog.debug.Logger&#39;);
<a name="line28"></a>goog.require(&#39;goog.events.Event&#39;);
<a name="line29"></a>goog.require(&#39;goog.events.EventTarget&#39;);
<a name="line30"></a>goog.require(&#39;goog.gears&#39;);
<a name="line31"></a>
<a name="line32"></a>
<a name="line33"></a>/**
<a name="line34"></a> * Class capture URLs to a ResourceStore on the LocalServer.
<a name="line35"></a> * @constructor
<a name="line36"></a> * @extends {goog.events.EventTarget}
<a name="line37"></a> * @param {string} name The name of the ResourceStore to capture the URLs to.
<a name="line38"></a> * @param {string?} requiredCookie  A cookie that must be present for the
<a name="line39"></a> *     managed store to be active. Should have the form &quot;foo=bar&quot;.
<a name="line40"></a> * @param {GearsResourceStore?} opt_localServer The LocalServer for gears.
<a name="line41"></a> */
<a name="line42"></a>goog.gears.UrlCapture = function(name, requiredCookie, opt_localServer) {
<a name="line43"></a>  goog.events.EventTarget.call(this);
<a name="line44"></a>
<a name="line45"></a>  /**
<a name="line46"></a>   * Name of resource store.
<a name="line47"></a>   * @type {string}
<a name="line48"></a>   * @private
<a name="line49"></a>   */
<a name="line50"></a>  this.storeName_ = goog.gears.makeSafeFileName(name);
<a name="line51"></a>  if (name != this.storeName_) {
<a name="line52"></a>    this.logger_.info(
<a name="line53"></a>        &#39;local store name &#39; + name + &#39;-&gt;&#39; + this.storeName_);
<a name="line54"></a>  }
<a name="line55"></a>
<a name="line56"></a>  /**
<a name="line57"></a>   * A cookie that must be present for the store to be active.
<a name="line58"></a>   * Should have the form &quot;foo=bar&quot;. String cast is a safety measure since
<a name="line59"></a>   * Gears behaves very badly when it gets an unexpected data type.
<a name="line60"></a>   * @type {string?}
<a name="line61"></a>   * @private
<a name="line62"></a>   */
<a name="line63"></a>  this.requiredCookie_ = requiredCookie ? String(requiredCookie) : null;
<a name="line64"></a>
<a name="line65"></a>  /**
<a name="line66"></a>   * The LocalServer for Gears.
<a name="line67"></a>   * @type {GearsLocalServer}
<a name="line68"></a>   * @private
<a name="line69"></a>   */
<a name="line70"></a>  this.localServer_ = opt_localServer ||
<a name="line71"></a>      goog.gears.getFactory().create(&#39;beta.localserver&#39;, &#39;1.0&#39;);
<a name="line72"></a>
<a name="line73"></a>  /**
<a name="line74"></a>   * Object mapping list of URIs to capture to capture id.
<a name="line75"></a>   * @type {Object}
<a name="line76"></a>   * @private
<a name="line77"></a>   */
<a name="line78"></a>  this.uris_ = {};
<a name="line79"></a>
<a name="line80"></a>  /**
<a name="line81"></a>   * Object mapping list of URIs that had errors in the capture to capture id.
<a name="line82"></a>   * @type {Object}
<a name="line83"></a>   * @private
<a name="line84"></a>   */
<a name="line85"></a>  this.errorUris_ = {};
<a name="line86"></a>
<a name="line87"></a>  /**
<a name="line88"></a>   * Object mapping number of URLs completed to capture id.
<a name="line89"></a>   * @type {Object}
<a name="line90"></a>   * @private
<a name="line91"></a>   */
<a name="line92"></a>  this.numCompleted_ = {};
<a name="line93"></a>};
<a name="line94"></a>goog.inherits(goog.gears.UrlCapture, goog.events.EventTarget);
<a name="line95"></a>
<a name="line96"></a>
<a name="line97"></a>/**
<a name="line98"></a> * Logger.
<a name="line99"></a> * @type {goog.debug.Logger}
<a name="line100"></a> * @private
<a name="line101"></a> */
<a name="line102"></a>goog.gears.UrlCapture.prototype.logger_ =
<a name="line103"></a>    goog.debug.Logger.getLogger(&#39;goog.gears.UrlCapture&#39;);
<a name="line104"></a>
<a name="line105"></a>
<a name="line106"></a>/**
<a name="line107"></a> * The ResourceStore for gears, used to capture URLs.
<a name="line108"></a> * @type {GearsResourceStore?}
<a name="line109"></a> * @private
<a name="line110"></a> */
<a name="line111"></a>goog.gears.UrlCapture.prototype.resourceStore_ = null;
<a name="line112"></a>
<a name="line113"></a>
<a name="line114"></a>/**
<a name="line115"></a> * Events fired during URL capture
<a name="line116"></a> * @enum {string}
<a name="line117"></a> */
<a name="line118"></a>goog.gears.UrlCapture.EventType = {
<a name="line119"></a>  URL_SUCCESS: &#39;url_success&#39;,
<a name="line120"></a>  URL_ERROR: &#39;url_error&#39;,
<a name="line121"></a>  COMPLETE: &#39;complete&#39;,
<a name="line122"></a>  ABORT: &#39;abort&#39;
<a name="line123"></a>};
<a name="line124"></a>
<a name="line125"></a>
<a name="line126"></a>/**
<a name="line127"></a> * Lazy initializer for resource store.
<a name="line128"></a> * @return {GearsResourceStore} Gears resource store.
<a name="line129"></a> * @private
<a name="line130"></a> */
<a name="line131"></a>goog.gears.UrlCapture.prototype.getResourceStore_ = function() {
<a name="line132"></a>  if (!this.resourceStore_) {
<a name="line133"></a>    this.logger_.info(&#39;creating resource store: &#39; + this.storeName_);
<a name="line134"></a>    this.resourceStore_ = this.localServer_[&#39;createStore&#39;](
<a name="line135"></a>        this.storeName_, this.requiredCookie_);
<a name="line136"></a>  }
<a name="line137"></a>  return this.resourceStore_;
<a name="line138"></a>};
<a name="line139"></a>
<a name="line140"></a>
<a name="line141"></a>/**
<a name="line142"></a> * Determine if the UrlCapture has been created.
<a name="line143"></a> * @return {boolean} True if it has been created.
<a name="line144"></a> */
<a name="line145"></a>goog.gears.UrlCapture.prototype.exists = function() {
<a name="line146"></a>  if (!this.resourceStore_) {
<a name="line147"></a>    this.logger_.info(&#39;opening resource store: &#39; + this.storeName_);
<a name="line148"></a>    this.resourceStore_ = this.localServer_[&#39;openStore&#39;](
<a name="line149"></a>        this.storeName_, this.requiredCookie_);
<a name="line150"></a>  }
<a name="line151"></a>  return !!this.resourceStore_;
<a name="line152"></a>};
<a name="line153"></a>
<a name="line154"></a>
<a name="line155"></a>/**
<a name="line156"></a> * Remove this resource store.
<a name="line157"></a> */
<a name="line158"></a>goog.gears.UrlCapture.prototype.removeStore = function() {
<a name="line159"></a>  this.logger_.info(&#39;removing resource store: &#39; + this.storeName_);
<a name="line160"></a>  this.localServer_[&#39;removeStore&#39;](this.storeName_, this.requiredCookie_);
<a name="line161"></a>  this.resourceStore_ = null;
<a name="line162"></a>};
<a name="line163"></a>
<a name="line164"></a>
<a name="line165"></a>/**
<a name="line166"></a> * Renames a Url that&#39;s been captured.
<a name="line167"></a> * @param {string|goog.Uri} srcUri The source Uri.
<a name="line168"></a> * @param {string|goog.Uri} dstUri The destination Uri.
<a name="line169"></a> */
<a name="line170"></a>goog.gears.UrlCapture.prototype.rename = function(srcUri, dstUri) {
<a name="line171"></a>  this.getResourceStore_()[&#39;rename&#39;](srcUri.toString(), dstUri.toString());
<a name="line172"></a>};
<a name="line173"></a>
<a name="line174"></a>
<a name="line175"></a>/**
<a name="line176"></a> * Starts the capture of the given URLs. Returns immediately, and fires events
<a name="line177"></a> * on success and error.
<a name="line178"></a> * @param {Array.&lt;string|goog.Uri&gt;} uris URIs to capture.
<a name="line179"></a> * @return {number} The id of the ResourceStore capture. Can be used to
<a name="line180"></a> * abort, or identify events.
<a name="line181"></a> */
<a name="line182"></a>goog.gears.UrlCapture.prototype.capture = function(uris) {
<a name="line183"></a>  var count = uris.length;
<a name="line184"></a>  this.logger_.fine(&#39;capture: count==&#39; + count);
<a name="line185"></a>  if (!count) {
<a name="line186"></a>    throw Error(&#39;No URIs to capture&#39;);
<a name="line187"></a>  }
<a name="line188"></a>
<a name="line189"></a>  // Convert goog.Uri objects to strings since Gears will throw an exception
<a name="line190"></a>  // for non-strings.
<a name="line191"></a>  var captureStrings = [];
<a name="line192"></a>  for (var i = 0; i &lt; count; i++) {
<a name="line193"></a>    captureStrings.push(uris[i].toString());
<a name="line194"></a>  }
<a name="line195"></a>
<a name="line196"></a>  var id = this.getResourceStore_()[&#39;capture&#39;](
<a name="line197"></a>      captureStrings, goog.bind(this.captureCallback_, this));
<a name="line198"></a>  this.logger_.fine(&#39;capture started: &#39; + id);
<a name="line199"></a>  this.uris_[id] = uris;
<a name="line200"></a>  this.errorUris_[id] = [];
<a name="line201"></a>  this.numCompleted_[id] = 0;
<a name="line202"></a>  return id;
<a name="line203"></a>};
<a name="line204"></a>
<a name="line205"></a>
<a name="line206"></a>/**
<a name="line207"></a> * Aborts the capture with the given id. Dispatches abort event.
<a name="line208"></a> * @param {number} captureId The id of the capture to abort, from #capture.
<a name="line209"></a> */
<a name="line210"></a>goog.gears.UrlCapture.prototype.abort = function(captureId) {
<a name="line211"></a>  this.logger_.fine(&#39;abort: &#39; + captureId);
<a name="line212"></a>
<a name="line213"></a>  // TODO Remove when Gears adds more rubust type handling.
<a name="line214"></a>  // Safety measure since Gears behaves very badly if it gets an unexpected
<a name="line215"></a>  // data type.
<a name="line216"></a>  if (typeof captureId != &#39;number&#39;) {
<a name="line217"></a>    throw Error(&#39;bad capture ID: &#39; + captureId);
<a name="line218"></a>  }
<a name="line219"></a>
<a name="line220"></a>  // Only need to abort if the capture is still in progress.
<a name="line221"></a>  if (this.uris_[captureId] || this.numCompleted_[captureId]) {
<a name="line222"></a>    this.logger_.info(&#39;aborting capture: &#39; + captureId);
<a name="line223"></a>    this.getResourceStore_()[&#39;abortCapture&#39;](captureId);
<a name="line224"></a>    this.cleanupCapture_(captureId);
<a name="line225"></a>    this.dispatchEvent(new goog.gears.UrlCapture.Event(
<a name="line226"></a>        goog.gears.UrlCapture.EventType.ABORT, captureId));
<a name="line227"></a>  }
<a name="line228"></a>};
<a name="line229"></a>
<a name="line230"></a>
<a name="line231"></a>/**
<a name="line232"></a> * Checks if a URL is captured.
<a name="line233"></a> * @param {string|goog.Uri} uri The URL to check.
<a name="line234"></a> * @return {boolean} true if captured, false otherwise.
<a name="line235"></a> */
<a name="line236"></a>goog.gears.UrlCapture.prototype.isCaptured = function(uri) {
<a name="line237"></a>  this.logger_.fine(&#39;isCaptured: &#39; + uri);
<a name="line238"></a>  return this.getResourceStore_()[&#39;isCaptured&#39;](uri.toString());
<a name="line239"></a>};
<a name="line240"></a>
<a name="line241"></a>
<a name="line242"></a>/**
<a name="line243"></a> * Removes the given URI from the store.
<a name="line244"></a> * @param {string|goog.Uri} uri The URI to remove from the store.
<a name="line245"></a> */
<a name="line246"></a>goog.gears.UrlCapture.prototype.remove = function(uri) {
<a name="line247"></a>  this.logger_.fine(&#39;remove: &#39; + uri);
<a name="line248"></a>  this.getResourceStore_()[&#39;remove&#39;](uri.toString());
<a name="line249"></a>};
<a name="line250"></a>
<a name="line251"></a>
<a name="line252"></a>/**
<a name="line253"></a> * This is the callback passed into ResourceStore.capture. It gets called
<a name="line254"></a> * each time a URL is captured.
<a name="line255"></a> * @param {string} url The url from gears, always a string.
<a name="line256"></a> * @param {boolean} success True if capture succeeded, false otherwise.
<a name="line257"></a> * @param {number} captureId The id of the capture.
<a name="line258"></a> * @private
<a name="line259"></a> */
<a name="line260"></a>goog.gears.UrlCapture.prototype.captureCallback_ = function(
<a name="line261"></a>    url, success, captureId) {
<a name="line262"></a>  this.logger_.fine(&#39;captureCallback_: &#39; + captureId);
<a name="line263"></a>
<a name="line264"></a>  if (!this.uris_[captureId] &amp;&amp; !this.numCompleted_[captureId]) {
<a name="line265"></a>    // This probably means we were aborted and then a capture event came in.
<a name="line266"></a>    this.cleanupCapture_(captureId);
<a name="line267"></a>    return;
<a name="line268"></a>  }
<a name="line269"></a>
<a name="line270"></a>  // Dispatch success/error event for the URL
<a name="line271"></a>  var eventUri = this.usesGoogUri_(captureId) ? new goog.Uri(url) : url;
<a name="line272"></a>  var eventType = null;
<a name="line273"></a>  if (success) {
<a name="line274"></a>    eventType = goog.gears.UrlCapture.EventType.URL_SUCCESS;
<a name="line275"></a>  } else {
<a name="line276"></a>    eventType = goog.gears.UrlCapture.EventType.URL_ERROR;
<a name="line277"></a>    this.errorUris_[captureId].push(eventUri);
<a name="line278"></a>  }
<a name="line279"></a>  this.dispatchEvent(new goog.gears.UrlCapture.Event(
<a name="line280"></a>      eventType, captureId, eventUri));
<a name="line281"></a>
<a name="line282"></a>  // Dispatch complete event for the entire capture, if necessary
<a name="line283"></a>  this.numCompleted_[captureId]++;
<a name="line284"></a>  if (this.numCompleted_[captureId] == this.uris_[captureId].length) {
<a name="line285"></a>    this.dispatchEvent(new goog.gears.UrlCapture.Event(
<a name="line286"></a>        goog.gears.UrlCapture.EventType.COMPLETE, captureId, null,
<a name="line287"></a>        this.errorUris_[captureId]));
<a name="line288"></a>    this.cleanupCapture_(captureId);
<a name="line289"></a>  }
<a name="line290"></a>};
<a name="line291"></a>
<a name="line292"></a>
<a name="line293"></a>/**
<a name="line294"></a> * Helper function to cleanup after a capture completes or is aborted.
<a name="line295"></a> * @private
<a name="line296"></a> * @param {number} captureId The id of the capture to clean up.
<a name="line297"></a> */
<a name="line298"></a>goog.gears.UrlCapture.prototype.cleanupCapture_ = function(captureId) {
<a name="line299"></a>  this.logger_.fine(&#39;cleanupCapture_: &#39; + captureId);
<a name="line300"></a>  delete this.uris_[captureId];
<a name="line301"></a>  delete this.numCompleted_[captureId];
<a name="line302"></a>  delete this.errorUris_[captureId];
<a name="line303"></a>};
<a name="line304"></a>
<a name="line305"></a>
<a name="line306"></a>/**
<a name="line307"></a> * Helper function to check whether a certain capture is using URIs of type
<a name="line308"></a> * String or type goog.Uri
<a name="line309"></a> * @private
<a name="line310"></a> * @param {number} captureId The id of the capture to check.
<a name="line311"></a> * @return {boolean} True if the capture uses goog.Uri, false if it uses string
<a name="line312"></a> * or there are no URIs associated with the capture.
<a name="line313"></a> */
<a name="line314"></a>goog.gears.UrlCapture.prototype.usesGoogUri_ = function(captureId) {
<a name="line315"></a>  if (this.uris_[captureId] &amp;&amp;
<a name="line316"></a>      this.uris_[captureId].length &gt; 0 &amp;&amp;
<a name="line317"></a>      this.uris_[captureId][0] instanceof goog.Uri) {
<a name="line318"></a>    return true;
<a name="line319"></a>  }
<a name="line320"></a>  return false;
<a name="line321"></a>};
<a name="line322"></a>
<a name="line323"></a>
<a name="line324"></a>/**
<a name="line325"></a> * An event dispatched by UrlCapture
<a name="line326"></a> * @constructor
<a name="line327"></a> * @extends {goog.events.Event}
<a name="line328"></a> * @param {goog.gears.UrlCapture.EventType} type Type of event to dispatch.
<a name="line329"></a> * @param {number} captureId The id of the capture that fired this event.
<a name="line330"></a> * @param {string|goog.Uri} opt_uri The URI for the event.
<a name="line331"></a> * @param {Array.&lt;string|goog.Uri&gt;} opt_errorUris The URIs that failed to load
<a name="line332"></a> * correctly.
<a name="line333"></a> */
<a name="line334"></a>goog.gears.UrlCapture.Event = function(type, captureId, opt_uri,
<a name="line335"></a>    opt_errorUris) {
<a name="line336"></a>  goog.events.Event.call(this, type);
<a name="line337"></a>
<a name="line338"></a>  /**
<a name="line339"></a>   * The id of the capture to dispatch the event for. This id is returned from
<a name="line340"></a>   * goog.gears.UrlCapture#capture
<a name="line341"></a>   * @type {number}
<a name="line342"></a>   */
<a name="line343"></a>  this.captureId = captureId;
<a name="line344"></a>
<a name="line345"></a>  /**
<a name="line346"></a>   * The URI the event concerns. Valid for URL_SUCCESS and URL_ERROR events.
<a name="line347"></a>   * @type {string|goog.Uri|null}
<a name="line348"></a>   */
<a name="line349"></a>  this.uri = opt_uri || null;
<a name="line350"></a>
<a name="line351"></a>  /**
<a name="line352"></a>   * A list of all the URIs that failed to load correctly. Valid for
<a name="line353"></a>   * COMPLETE event.
<a name="line354"></a>   * @type {Array.&lt;string|goog.Uri&gt;}
<a name="line355"></a>   */
<a name="line356"></a>  this.errorUris = opt_errorUris || [];
<a name="line357"></a>};
<a name="line358"></a>goog.inherits(goog.gears.UrlCapture.Event, goog.events.Event);
</pre>


</body>
</html>
